package model.dao;

import java.util.List;

import model.Game;

public class GameDAO extends DAO<Game>
{
	public GameDAO( ) 
	{
		super( );
	}
	
	public Game busca( int nId )
	{
		try
		{
			return manager.find( Game.class, nId );
		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}
		
		return null;
	}
	
	public List<Game> busca( String strQuery )
	{
		try
		{
			return manager.createQuery( strQuery, Game.class ).getResultList( );
		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}
		
		return null;
	}
	
	public boolean save( Game game )
	{
		try
		{
			manager.getTransaction( ).begin( );
			manager.persist( game );
			manager.getTransaction( ).commit( );
			
			return true;
		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}
		
		return false;
	}

	public synchronized int save( List<Game> games )
	{
		try
		{
			manager.getTransaction( ).begin( );
			
			for( Game game : games )
			{
				manager.persist( game );
			}
			
			manager.getTransaction( ).commit( );

			return 1;
		}
		catch( Exception e )
		{
			e.printStackTrace( );
			
			return -1;
		}
	}
}